﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ConsolidationTool.Common.Entities;
using System.Collections.Generic;
using ConsolidationTool.Common;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.WebParts.ControlTemplates;

using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace ConsolidationTool.WebParts.Layouts.CReport.Brochure
{
    public partial class P15QSCC : FSReportBase
    {

        public override ControlTemplates.CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }

        public override Repeater BaseGAP
        {
            get
            {
                return RPGAP;
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
      
        }
      
        void InitBaseInfo()
        {
            // lbCompanyName.Text = Constants.Company_Name;
            TopSelector.yearTextBox.Text = Common.BusinessService.GetConfiguration(Constants.Flash_Input_Year);
            TopSelector.monthTextBox.Text = Common.BusinessService.GetConfiguration(Constants.Flash_Input_Month);


            switch (TopSelector.monthTextBox.Text)
            {
                case "1":
                    LabelCurrentMonth.Text = "MONTH - January " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - January " + TopSelector.yearTextBox.Text;
                    break;
                case "2":
                    LabelCurrentMonth.Text = "MONTH - February " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - February " + TopSelector.yearTextBox.Text;
                    break;
                case "3":
                    LabelCurrentMonth.Text = "MONTH - March " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - March " + TopSelector.yearTextBox.Text;
                    break;
                case "4":
                    LabelCurrentMonth.Text = "MONTH - April " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - April " + TopSelector.yearTextBox.Text;
                    break;
                case "5":
                    LabelCurrentMonth.Text = "MONTH - May " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - May " + TopSelector.yearTextBox.Text;
                    break;
                case "6":
                    LabelCurrentMonth.Text = "MONTH - June " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - June " + TopSelector.yearTextBox.Text;
                    break;
                case "7":
                    LabelCurrentMonth.Text = "MONTH - July " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - July " + TopSelector.yearTextBox.Text;
                    break;
                case "8":
                    LabelCurrentMonth.Text = "MONTH - August " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - August " + TopSelector.yearTextBox.Text;
                    break;
                case "9":
                    LabelCurrentMonth.Text = "MONTH - September " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - September " + TopSelector.yearTextBox.Text;
                    break;
                case "10":
                    LabelCurrentMonth.Text = "MONTH - October " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - October " + TopSelector.yearTextBox.Text;
                    break;
                case "11":
                    LabelCurrentMonth.Text = "MONTH - November " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - November " + TopSelector.yearTextBox.Text;
                    break;
                case "12":
                    LabelCurrentMonth.Text = "MONTH - December " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - December " + TopSelector.yearTextBox.Text;
                    break;
                default:
                    break;
            }
        }


        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            base.GenerateReport_Click(sender, e);
             

            switch (TopSelector.monthTextBox.Text)
            {
                case "1":
                    LabelCurrentMonth.Text = "MONTH - January " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - January " + TopSelector.yearTextBox.Text;
                    break;
                case "2":
                    LabelCurrentMonth.Text = "MONTH - February " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - February " + TopSelector.yearTextBox.Text;
                    break;
                case "3":
                    LabelCurrentMonth.Text = "MONTH - March " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - March " + TopSelector.yearTextBox.Text;
                    break;
                case "4":
                    LabelCurrentMonth.Text = "MONTH - April " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - April " + TopSelector.yearTextBox.Text;
                    break;
                case "5":
                    LabelCurrentMonth.Text = "MONTH - May " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - May " + TopSelector.yearTextBox.Text;
                    break;
                case "6":
                    LabelCurrentMonth.Text = "MONTH - June " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - June " + TopSelector.yearTextBox.Text;
                    break;
                case "7":
                    LabelCurrentMonth.Text = "MONTH - July " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - July " + TopSelector.yearTextBox.Text;
                    break;
                case "8":
                    LabelCurrentMonth.Text = "MONTH - August " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - August " + TopSelector.yearTextBox.Text;
                    break;
                case "9":
                    LabelCurrentMonth.Text = "MONTH - September " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - September " + TopSelector.yearTextBox.Text;
                    break;
                case "10":
                    LabelCurrentMonth.Text = "MONTH - October " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - October " + TopSelector.yearTextBox.Text;
                    break;
                case "11":
                    LabelCurrentMonth.Text = "MONTH - November " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - November " + TopSelector.yearTextBox.Text;
                    break;
                case "12":
                    LabelCurrentMonth.Text = "MONTH - December " + TopSelector.yearTextBox.Text;
                    LabelYTD.Text = "YTD - December " + TopSelector.yearTextBox.Text;
                    break;
                default:
                    break;
            }

            //邦定current月数据

            BindLastMonth(TopSelector.drpEntity.SelectedValue, TopSelector.yearTextBox.Text, TopSelector.monthTextBox.Text);


        }

        public override void ExportReport_Click(object sender, EventArgs e)
        {
            P6PL colData = null;
            colData = new P6PL();
            colData.row01 = Label01_Day02.Text;
            colData.row02 = Label01_Day03.Text;
            colData.row03 = Label01_Day04.Text;
            colData.row04 = Label01_Day05.Text;
            colData.row05 = Label02_Day02.Text;
            colData.row06 = Label02_Day03.Text;
            colData.row07 = Label02_Day04.Text;
            colData.row08 = Label02_Day05.Text;
            
            var RQReportDataSource = ViewState["ART_GAP_DataSource"] as List<RQDataItem>;

            colData.repObj = RQReportDataSource;

            string fileName = TopSelector.yearTextBox.Text + TopSelector.monthTextBox.Text + Constants._Brochure_P15_PRIFIX + TopSelector.drpEntity.SelectedValue + ".xlsx";
            BusinessService.CreateP15Excel(fileName, LabelCurrentMonth.Text, LabelYTD.Text, colData);
 
            string reportUrl = SPContext.Current.Site.Url + "/" + Constants._EXCEL_Library + "/" + fileName;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", string.Format("<script>window.open('" + reportUrl + "');</script>"));

        }


        void BindLastMonth(string coCd, string year, string month)
        {
            Label01_Day01.Text = "Number of claims";
            Label02_Day01.Text = "Rate of claims (%)";


            int y = Convert.ToInt32(year) - 1;
            string yearN_1 = Convert.ToString(y);
            SPListItem item = Common.BusinessService.GetItem(coCd, year, month, "Quality&Safety");
            SPListItem itemN_1 = Common.BusinessService.GetItem(coCd, yearN_1, month, "Quality&Safety");

            if (item != null)
            {
                Label01_Day03.Text = item["QS01030302"].ToString();
                Label02_Day03.Text = item["QS01030401"].ToString();
            }
            else
            {
                Label01_Day03.Text = "0";
                Label02_Day03.Text = "0";
            }

            if (itemN_1 != null)
            {
                Label01_Day02.Text = itemN_1["QS01030302"].ToString();
                Label02_Day02.Text = itemN_1["QS01030401"].ToString();
            }
            else
            {
                Label01_Day02.Text = "0";
                Label02_Day02.Text = "0";
            }

            int m = Convert.ToInt32(month);

            decimal currentC01 = 0;
            decimal currentC02 = 0;
            decimal currentN01 = 0;
            decimal currentN02 = 0;

            for (int i = 1; i <= m; i++)
            {
                string strMonth = i.ToString();
                SPListItem itemCurrent = Common.BusinessService.GetItem(coCd, year, strMonth, "Quality&Safety");
                SPListItem itemN = Common.BusinessService.GetItem(coCd, yearN_1, strMonth, "Quality&Safety");

                if (itemCurrent != null)
                {
                    currentC01 = currentC01 + Convert.ToDecimal(itemCurrent["QS01030301"].ToString());
                    currentC02 = currentC02 + Convert.ToDecimal(itemCurrent["QS01030302"].ToString());
                }
                if (itemN != null)
                {
                    currentN01 = currentN01 + Convert.ToDecimal(itemN["QS01030301"].ToString());
                    currentN02 = currentN02 + Convert.ToDecimal(itemN["QS01030302"].ToString());
                }
            }
            Label01_Day04.Text = Convert.ToString(currentN02);
            Label01_Day05.Text = Convert.ToString(currentC02);

            if (currentN01 != 0)
            {
                Label02_Day04.Text = (currentN02 / currentN01*100).ToString("n");
            }
            else
            {
                Label02_Day04.Text = "0.00";
            }
            if (currentC01 != 0)
            {
                Label02_Day05.Text = (currentC02 / currentC01 * 100).ToString("n");
            }
            else
            {
                Label02_Day05.Text = "0.00";
            }
            
        }    

    }
}
